//
//  SceneDelegate.m
//  SampleApp2
//
//  Created by jsjxsy on 2023/6/5.
//

#import "MineController/Controller/MineViewController.h"
#import "NewsController/Controller/GTNewsViewController.h"
#import "RecommendController/Controller/RecommendViewController.h"
#import "SceneDelegate.h"
#import "Utils/GTLocation.h"
#import "VideoController/Controller/VideoViewController.h"
//项目的pch文件中
#import <CocoaLumberjack/CocoaLumberjack.h>
#import <execinfo.h>
#import "Utils/GTNotification.h"
//定义Log等级
static const DDLogLevel ddLogLevel = DDLogLevelInfo;

@interface SceneDelegate ()<UITabBarControllerDelegate>
-(void)_caughtException;
@end

@implementation SceneDelegate


/// <#Description#>
/// @param scene <#scene description#>
/// @param session <#session description#>
/// @param connectionOptions <#connectionOptions description#>
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).


    UIWindowScene *windowScene = (UIWindowScene *)scene;

    self.window = [[UIWindow alloc]initWithWindowScene:windowScene];
    self.window.frame = windowScene.coordinateSpace.bounds;
    //	self.window.frame = [[UIScreen mainScreen]bounds];

    UITabBarController *tabBarController = [[UITabBarController alloc]init];
    tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
    tabBarController.delegate = self;

    GTNewsViewController *newsViewController = [[GTNewsViewController alloc]init];
    RecommendViewController *recommendViewController = [[RecommendViewController alloc]init];
    VideoViewController *videoViewController = [[VideoViewController alloc]init];
    MineViewController *mineViewController = [[MineViewController alloc]init];

    [tabBarController setViewControllers:@[newsViewController, videoViewController, recommendViewController, mineViewController]];

    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:tabBarController];

    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];

    [[GTLocation locationManager] checkLocationAuthorization];
	[[GTNotification notificationManager] checkNotificationAuthorization];
	
    //添加控制台logger
    [DDLog addLogger:[DDOSLogger sharedInstance]];
    //打印，跟NSLog用法一致
    DDLogError(@"DDLogError");
    DDLogWarn(@"DDLogWarn");
    DDLogInfo(@"My name is = %@", @"谦言忘语");
    DDLogDebug(@"I am %d years old", 18);
    DDLogVerbose(@"DDLogVerbose");
	DDLog(@"DDLogWarn");
	[self _caughtException];
//	[@[].mutableCopy addObject:nil];
	
	[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
	
	
}
#pragma mark - remote push
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
	NSLog(@"");
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
	NSLog(@"");
}



#pragma mark - cash
-(void)_caughtException{
	//NSException
	NSSetUncaughtExceptionHandler(HandleNSException);
	//signal
	signal(SIGABRT,SignalExceptionHandler);
}

void SignalExceptionHandler(int signal){
	void* callstack[128];
	int frames = backtrace(callstack, 128);
	char **strs = backtrace_symbols(callstack, frames);
	NSMutableArray *backstrace = [NSMutableArray arrayWithCapacity:frames];
	for (int i=0; i<frames; i++) {
		[backstrace addObject:[NSString stringWithUTF8String:strs[i]]];
	}
	free(strs);
}
void HandleNSException(NSException *exception){
	NSLog(@"name = %@",exception.name);
	NSLog(@"reason = %@",exception.reason);
	NSLog(@"userInfo = %@",exception.userInfo);
	NSLog(@"description = %@",exception.description);
}


#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController API_AVAILABLE(ios(3.0)) {
    NSLog(@"shouldSelectViewController");
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"didSelectViewController");
}

- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}

- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}

- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}

- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}

- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}

@end
